package course

import (
	"census/common/consts"
	"census/common/records"
	"fmt"
	"github.com/golib/mysql"
	"github.com/golib/sms"
	"census/conf"
	"census/context"
)

func init() {
	context.RegisterModule(consts.ModuleCourseBeginSms, SmsReminderCourseBeginAction, "courseId", "classId")
	context.RegisterModule(consts.ModuleCourseEndSms, SmsReminderCourseEndAction, "classId", "courseId")
}

var courseTypeEnum = map[int]string{
	0: "AI直播课",
	1: "素质拓展课",
	2: "考试课",
}

//课程开始
func SmsReminderCourseBeginAction(ctx *context.Parser) {
	classId, err := ctx.GetIntParam("classId")
	if err != nil {
		panic("classId is not int")
	}
	courseId, err := ctx.GetIntParam("courseId")
	if err != nil {
		panic("courseId is not int")
	}
	var db = mysql.GetInstance(false)
	var classRecord = db.Find(records.RecordNameOnlineClassInfo).
		Select("class_id", "class_name", "teacher_id", "student_count").
		Where("class_id", "=", classId).
		Execute().Fetch()
	if classRecord == nil {
		panic("class record not exist")
	}
	var courseRecord = db.Find(records.RecordNameCmsCourseInfo).
		Select("course_id", "course_name", "type").Where("course_id", "=", courseId).
		Execute().Fetch()
	if courseRecord == nil {
		panic("course record not exist")
	}

	var classCourseRecord = db.Find(records.RecordNameOnlineClassCourse).
		Select("start_time", "end_time").
		Where("class_id", "=", classId).
		AndWhere("course_id", "=", courseId).Execute().Fetch()
	if classCourseRecord == nil {
		panic("class course record not exist")
	}
	var classInfo = classRecord.(*records.OnlineClassInfo)
	var courseInfo = courseRecord.(*records.CmsCourseInfo)
	var classCourseInfo = classCourseRecord.(*records.OnlineClassCourse)
	var teacherRecord = db.Find(records.RecordNameOnlineUserInfo).
		Select("user_name").Where("user_id", "=", classInfo.TeacherId).
		Execute().Fetch()
	if teacherRecord == nil {
		panic("teacher record not exist")
	}
	var teacherInfo = teacherRecord.(*records.OnlineUserInfo)
	var msg = fmt.Sprintf("老师:(%v)，课程名称:(%v),开始时间:(%v),结束时间:(%v),课程类型:(%v),班级名称:(%v),班级总人数:(%v)",
		teacherInfo.UserName, courseInfo.CourseName, classCourseInfo.StartTime, classCourseInfo.EndTime, getCourseTypeEnum(courseInfo.Type), classInfo.ClassName, classInfo.StudentCount)
	smsSend("课程开始", msg)

}

func getCourseTypeEnum(courseType int) (name string) {
	if n, ok := courseTypeEnum[courseType]; ok {
		name = n
		return
	}
	return "未知类型"
}

//李延宝:15210687831
//张昊波:13811762727
//王萌:18600416086
//赵瑞颀:17310015170
//陈云南:18500038283
//庄彦旭:15010111255
//潘锐:13810549790
//田岳:18601013193
//佟玲:17600859365
//余绍龙: 18612402834
//王慧:  18610470651

func smsSend(tag, msgBody string) {
	var msg = fmt.Sprintf("【AIClass提醒】: [%v] %v", tag, msgBody)
	fmt.Println(msg)
	code := fmt.Sprintf("【香蕉学堂】：%s。", msg)
	if len(conf.Appcfg.Params.CourseSmsInternalMobileList) > 0 {
		var mobile = conf.Appcfg.Params.CourseSmsInternalMobileList
		sms.SendVerifyCode(code, mobile...)
	}

}

//课程结束
func SmsReminderCourseEndAction(ctx *context.Parser) {
	classId, err := ctx.GetIntParam("classId")
	if err != nil {
		panic("classId is not int")
	}
	courseId, err := ctx.GetIntParam("courseId")
	if err != nil {
		panic("courseId is not int")
	}
	var db = mysql.GetInstance(false)
	var classRecord = db.Find(records.RecordNameOnlineClassInfo).
		Select("class_id", "class_name", "teacher_id", "student_count").
		Where("class_id", "=", classId).
		Execute().Fetch()
	if classRecord == nil {
		panic("class record not exist")
	}
	var courseRecord = db.Find(records.RecordNameCmsCourseInfo).
		Select("course_id", "course_name", "type").Where("course_id", "=", courseId).
		Execute().Fetch()
	if courseRecord == nil {
		panic("course record not exist")
	}

	var classCourseRecord = db.Find(records.RecordNameOnlineClassCourse).
		Select("start_time", "end_time").
		Where("class_id", "=", classId).
		AndWhere("course_id", "=", courseId).Execute().Fetch()
	if classCourseRecord == nil {
		panic("class course record not exist")
	}
	var classInfo = classRecord.(*records.OnlineClassInfo)
	var courseInfo = courseRecord.(*records.CmsCourseInfo)
	var classCourseInfo = classCourseRecord.(*records.OnlineClassCourse)
	var teacherRecord = db.Find(records.RecordNameOnlineUserInfo).
		Select("user_name").Where("user_id", "=", classInfo.TeacherId).
		Execute().Fetch()
	if teacherRecord == nil {
		panic("teacher record not exist")
	}
	var teacherInfo = teacherRecord.(*records.OnlineUserInfo)
	var studentCourseListRecord = db.Find(records.RecordNameOnlineStudentCourse).
		Select("student_id", "status").Where("class_id", "=", classId).
		AndWhere("course_id", "=", courseId).Execute().FetchAll()
	var account, online, offline, absent, rate int
	if studentCourseListRecord.Len() != 0 {
		for _, o := range studentCourseListRecord.AllRecord() {
			c := o.(*records.OnlineStudentCourse)
			account++
			switch c.Status {
			case 0:
				absent++
			case 1:
				offline++
			case 2:
				online++
			}
		}
		rate = int(absent * 100 / account)
	}
	var msg = fmt.Sprintf("老师:(%v)，课程名称:(%v),开始时间:(%v),结束时间:(%v),课程类型:(%v),班级名称:(%v),班级总人数:(%v),结算总人数:(%v),实际上课人数:(%v),中途掉线人数:(%v),缺勤人数:(%v),缺勤率:(%v%%)",
		teacherInfo.UserName, courseInfo.CourseName, classCourseInfo.StartTime, classCourseInfo.EndTime, getCourseTypeEnum(courseInfo.Type), classInfo.ClassName, classInfo.StudentCount, account, online, offline, absent, rate)
	smsSend("课程结束", msg)

}
